package io.github.hadyang.leetcode.offer;

import java.util.ArrayList;
import java.util.LinkedList;

import io.github.hadyang.leetcode.TreeNode;

/** @author haoyang.shi */
public class PrintFromTopToBottom {

  public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
    ArrayList<Integer> res = new ArrayList<>();
    LinkedList<TreeNode> nodeQueue = new LinkedList<>();

    if (root == null) {
      return res;
    }

    nodeQueue.addLast(root);

    while (!nodeQueue.isEmpty()) {
      TreeNode node = nodeQueue.pollFirst();
      if (node == null) {
        continue;
      }

      nodeQueue.addLast(node.left);
      nodeQueue.addLast(node.right);

      res.add(node.val);
    }

    return res;
  }
}
